<template>
  <div class="app-container">
    <el-form v-show="state.showSearch" :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="系统名称" prop="versionName">
        <el-input v-model="queryParams.versionName" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="状态" prop="pushStatus">
        <el-select v-model="queryParams.pushStatus" placeholder="请选择" clearable>
          <el-option
            v-for="dict in version_push_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="&nbsp;">
        <el-button @click="handleSearch(false)">重置</el-button>
        <el-button type="primary" @click="handleSearch(true)">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:version:add']"
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd()"
          >新增</el-button
        >
      </el-col>
      <right-toolbar v-model:showSearch="state.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="state.loading" :data="tableList">
      <el-table-column label="版本号" align="center" prop="versionNo" min-width="100" />
      <el-table-column
        label="系统名称"
        align="center"
        prop="versionName"
        min-width="120"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="内部版本号"
        align="center"
        prop="innerVersionNo"
        min-width="100"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="设备类型"
        align="center"
        prop="machineType"
        :show-overflow-tooltip="true"
        min-width="100"
      >
        <template #default="scope">
          <dict-tag :options="device_type" :value="scope.row.machineType" />
        </template>
      </el-table-column>
      <el-table-column label="系统大小" align="center" prop="size" :show-overflow-tooltip="true" />
      <el-table-column
        label="推送状态"
        align="center"
        prop="pushStatus"
        :show-overflow-tooltip="true"
        min-width="100"
      >
        <template #default="scope">
          <dict-tag :options="version_push_status" :value="scope.row.pushStatus" />
        </template>
      </el-table-column>
      <el-table-column label="推送时间" align="center" prop="pushTime" min-width="100" />
      <el-table-column label="操作" align="center" fixed="right" min-width="200">
        <template #default="scope">
          <el-button
            v-hasPermi="['system:version:push']"
            link
            type="primary"
            @click="handleTableAction(scope.row)"
            >版本推送</el-button
          >
          <el-button link type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="state.total > 0"
      :total="state.total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList()"
    />

    <operate ref="operateRef" :operType="state.operType" @updateList="getList()" />
    <versionPush ref="versionPushRef" :operType="state.operType" @updateList="getList()" />
  </div>
</template>

<script setup name="GoodsSort">
import versionPush from './templates/versionPush.vue'
import operate from './templates/operate.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { deleteVersionApi, getapkVersionList } from '@/api/advert'
import { reactive, ref } from 'vue'

const queryRef = ref(null)
const { proxy } = getCurrentInstance()
const { version_push_status } = proxy.useDict('version_push_status')
const { device_type } = proxy.useDict('device_type')

const state = reactive({
  currentId: null,
  showSearch: true,
  total: 1,
  loading: false,
  operType: ''
})
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  versionName: null,
  pushStatus: null
})
const tableList = ref([])

function handleSearch(confirm) {
  !confirm && queryRef.value.resetFields()
  getList(true)
}
async function getList(refresh) {
  state.loading = true
  if (refresh) queryParams.value.pageNum = 1
  try {
    const res = await getapkVersionList(queryParams.value)
    tableList.value = res.rows
    state.total = res.total
  } finally {
    state.loading = false
  }
}

/**table Action */
function handleAdd() {
  state.operType = 'add'
  state.data = {}
  proxy.$refs['operateRef'].show('add')
}
function handleTableAction(row) {
  state.operType = 'push'
  proxy.$refs['versionPushRef'].show('push', row.versionNo)
}
function handleDelete(row) {
  ElMessageBox.confirm('版本记录将会被删除', '操作', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'error'
  })
    .then(() => {
      deleteVersionApi(row.versionNo).then(() => {
        ElMessage({
          type: 'success',
          message: '操作成功'
        })
        getList()
      })
    })
    .catch(() => {})
}

/**init */
function init() {
  getList()
}

init()
</script>

<style lang="scss" scoped>
:deep(.el-dialog__wrapper) {
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
}

:deep(.el-dialog) {
  margin: 0 auto;
  max-height: 100%;
  overflow-y: auto;
}
:deep(.el-dialog:not(.is-fullscreen)) {
  margin-top: 30vh !important;
}
</style>
